package com.mz.mattress.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.admin.api.req.PlanDateQueryReq;
import com.mz.mattress.admin.mapper.PlanDateMapper;
import com.mz.mattress.admin.service.PlanDateService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.PlanDate;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Slf4j
@Service
public class PlanDateServiceImpl implements PlanDateService {

    @Autowired
    private PlanDateMapper planDateMapper;

    @Override
    public Result<IPage<PlanDate>> listPage(PlanDateQueryReq req) {
        Page<PlanDate> listPage = planDateMapper.listPage(req.newPage(), req.getDates());
        return Result.ok(listPage);
    }

    @Override
    public Result<PlanDate> getById(Integer id) {
        PlanDate planDate = planDateMapper.selectById(id);
        return Result.ok(planDate);
    }

    @Override
    public Result add(PlanDate planDate) {

        if (planDate.getOrder1Count() == null
                || planDate.getOrder3Count() == null
                || planDate.getOrder6Count() == null
                || planDate.getOrder10Count() == null) {
            return Result.fail("参数错误");
        }

        PlanDate data = planDateMapper.selectByDates(planDate.getDates());
        if (data != null) {
            return Result.fail("已有记录");
        }

        Integer total = planDate.getOrder1Count() +
                planDate.getOrder3Count() +
                planDate.getOrder6Count() +
                planDate.getOrder10Count();

        planDate.setOrderTotal(total);
        planDateMapper.insert(planDate);

        return Result.ok();
    }

    @Override
    public Result updateById(PlanDate planDate) {

        if (planDate.getOrder1Count() == null
                || planDate.getOrder3Count() == null
                || planDate.getOrder6Count() == null
                || planDate.getOrder10Count() == null) {
            return Result.fail("参数错误");
        }

        Integer total = planDate.getOrder1Count() +
                planDate.getOrder3Count() +
                planDate.getOrder6Count() +
                planDate.getOrder10Count();

        planDate.setOrderTotal(total);
        //日期不能改
        planDate.setDates(null);
        planDateMapper.updateById(planDate);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        planDateMapper.deleteBatchIds(ids);
        return Result.ok();
    }

}
